Best way to wrap a struct with a `const char*` field · pybind pybind11 · Discussion #4556 · GitHub

您所在的位置:网站首页 python const Best way to wrap a struct with a `const char*` field · pybind pybind11 · Discussion #4556 · GitHub

Best way to wrap a struct with a `const char*` field · pybind pybind11 · Discussion #4556 · GitHub

#Best way to wrap a struct with a `const char*` field · pybind pybind11 · Discussion #4556 · GitHub| 来源: 网络整理| 查看: 265

Hello,

I am creating python binding for a C++ struct that I cannot modify, as below:

struct Information { const char* name; // other fields }

Naturally we need to create its instance as below in python:

info = Information("test", ...)

Therefore, I choose to implement the binding code as below:

py::class_(m, "Information") .def( py::init( [](const char* name) { // deep copy is required. size_t const size = strlen(name) + 1; char* buffer = new char[size]; memcpy(buffer, name, size); return new Information{ buffer }; })) .def_readonly("name", &Information::name);

This will have memory leak because the memory allocation doesn't have a chance to be released. What is the best way to create python binding for this struct please?

Thanks in advance.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3